﻿using Flare.DB.CeEntity;
using System;
using System.Windows;
using System.Windows.Controls;

namespace Flare.Pages.ITContract
{
    public partial class Provider : UserControl, ITypicalPage
    {
        public string name
        {
            get { return "Поставщики услуг"; }
        }

        public Provider()
        {
            InitializeComponent();
        }

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            var data = new contract_provider()
            {
                provider = provider.Text,
                provider_address = provider_address.Text,
                default_contract_number = default_contract_number.Text
            };

            try
            {
                new DB.contract_providers().Create(data);
                ProvidersList.SelectedItem = null;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }
            glob.kernel.RefreshResourceByName("contract_providersDataSet");
        }

        private void ProvidersList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ProvidersList.SelectedIndex == -1)
            {
                ButtonSave.Visibility = System.Windows.Visibility.Collapsed;
                ButtonDelete.Visibility = System.Windows.Visibility.Collapsed;
                ButtonClose.Visibility = System.Windows.Visibility.Collapsed;
                ButtonAdd.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                ButtonSave.Visibility = System.Windows.Visibility.Visible;
                ButtonDelete.Visibility = System.Windows.Visibility.Visible;
                ButtonClose.Visibility = System.Windows.Visibility.Visible;
                ButtonAdd.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            if (ProvidersList.SelectedIndex == -1) return;

            var data = new contract_provider()
            {
                id_provider = (int)ProvidersList.SelectedValue,
                provider = provider.Text,
                provider_address = provider_address.Text,
                default_contract_number = default_contract_number.Text
            };

            try
            {
                new DB.contract_providers().Edit(data);
                ProvidersList.SelectedItem = null;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("contract_providersDataSet");
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (ProvidersList.SelectedIndex == -1) return;

            try
            {
                new DB.contract_providers().Delete((int)ProvidersList.SelectedValue);
                ProvidersList.SelectedItem = null;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("contract_providersDataSet");
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            ProvidersList.SelectedItem = null;
        }
    }
}
